/*
* $Id: InspectStore.java 234 2010-12-29 13:37:20Z volokaleks@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db;

import java.io.*;

/**
 * Family inspection for hive database
 * @author lexus
 */
public class InspectStore extends DBStore
{
    /**
     * Container of inspection records
     * @author lexus
     */
    public class InspectRecord implements DBRecord
    {
        /** Identificator of common inspection */
        public int idJournal;
        /** Identificator of hive */
        public int idHousing;
        /** name hive if identificator is null */
        public String nameHousing;
        /** Amount honey */
        public int honey;
        /** Amount brood */
        public int brood;
        /** Amount open brood */
        public int openBrood;
        /** Amount close brood */
        public int closeBrood;
        /** Amount goods honey */
        public int goodsHoney;

        /**
         * Constructor
         */
        public InspectRecord() {
            nameHousing = new String();
        }

        /**
         * Get byte array of record
         * @return byte array
         */
        public byte[] getData() {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                dos.writeInt(idJournal);
                dos.writeInt(idHousing);
                dos.writeUTF(nameHousing);
                dos.writeInt(honey);
                dos.writeInt(brood);
                dos.writeInt(openBrood);
                dos.writeInt(closeBrood);
                dos.writeInt(goodsHoney);
                return baos.toByteArray();
            } catch (IOException ex) {}
            return null;
        }

        /**
         * Sets byte array record.
         * Array will be parsed and all fields of it will be filled
         * @param data byte array
         * @return number read byte
         */
        public int setData(byte[] data) {
            int readData = 0;
            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                idJournal = dis.readInt();
                idHousing = dis.readInt();
                nameHousing = dis.readUTF();
                honey = dis.readInt();
                brood = dis.readInt();
                openBrood = dis.readInt();
                closeBrood = dis.readInt();
                goodsHoney = dis.readInt();
                readData = data.length - dis.available();
            } catch (IOException ex) {}
            return readData;
        }
    }

    /**
     * Constructor
     */
    public InspectStore() {
        super("db_inspect");
    }

    /**
     * Gets new record
     * @return new record
     */
    public DBRecord getRecord() {
        return new InspectRecord();
    }
}
